﻿using BP.Difference;
using BP.Sys;
using BP.Web;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BP.DA
{
    public class SQLDBSrc
    {
        public SQLDBSrc() { }

        public static string Gener_DBSrc(string mark, Hashtable ht = null)
        {
            string key = null;
            if (ht != null && ht.ContainsKey("Key") == true)
                key = ht["Key"].ToString();
            //枚举列表
            if (mark.Equals("DBSrc_EnumMain") == true) { 
                if (SystemConfig.CCBPMRunModel == CCBPMRunModel.Single)
                    return "SELECT No,Name FROM Sys_EnumMain WHERE Name IS NOT NULL ";
                return "SELECT No,Name FROM Sys_EnumMain WHERE Name IS NOT NULL AND OrgNo='" + WebUser.OrgNo + "' ";
            }

            //字典
            if (mark.Equals("DBSrc_SQLSFTable") == true)
            {
                if (SystemConfig.CCBPMRunModel == CCBPMRunModel.Single)
                    return "SELECT No, Name, FK_SFDBSrc as GroupNo FROM Sys_SFTable";
                return "SELECT No,Name, FK_SFDBSrc as GroupNo FROM Sys_SFTable WHERE OrgNo='" + WebUser.OrgNo + "' ";
            }

            //字典
            if (mark.Equals("DBSrc_SFTableSysDict") == true)
            {
                if (SystemConfig.CCBPMRunModel == CCBPMRunModel.Single)
                    return " SELECT No, Name, FK_SFDBSrc as GroupNo FROM Sys_SFTable WHERE DBSrcType='SysDict' ";
                return "SELECT No,Name, FK_SFDBSrc as GroupNo FROM Sys_SFTable WHERE OrgNo='" + WebUser.OrgNo + "' AND DBSrcType='SysDict'  ";
            }
            //数据源
            if (mark.Equals("DBSrc_SFTable") == true)
            {
                if (SystemConfig.CCBPMRunModel == CCBPMRunModel.Single)
                    return "SELECT No,Name FROM Sys_SFTable";
                else
                    return "SELECT No,Name FROM Sys_SFTable  WHERE OrgNo='" + WebUser.OrgNo + "'";
            }
            //枚举
            if (mark.Equals("DBSrc_Enums") == true)
            {
                if (SystemConfig.CCBPMRunModel == CCBPMRunModel.Single)
                    return "SELECT No,Name FROM Sys_EnumMain";
                else
                    return "SELECT No,Name FROM Sys_EnumMain WHERE OrgNo='" + WebUser.OrgNo + "'";
            }
            //数据源
            if (mark.Equals("DBSrc_DBSrc") == true)
            {
                return "SELECT No,Name FROM Sys_SFDBSrc ";
            }
            //查询
            if (mark.Equals("DBSrc_DBSFSearch") == true)
            {
                return "SELECT No,Name, FK_SFDBSrc FROM Sys_SFSearch";
            }
            //过程
            if (mark.Equals("DBSrc_SFProc") == true)
            {
                return "SELECT No,Name, FK_SFDBSrc FROM Sys_SFProc";
            }

            //节点属性 绑定字典 AccepterRoleBindSFTable 请选择字典
            if (mark.Equals("DBSrc_BindSFTable") == true)
            {
                return "SELECT No,Name FROM Sys_SFTable WHERE CodeStruct=0";
            }
            //ARWebAPI 绑定WebAPI
            if (mark.Equals("DBSrc_WebAPISFTable") == true)
            {
                return "SELECT No,Name From Sys_SFTable WHERE DBSrcType='WebAPI'";
            }
            //表单条件 CondFrmEnum const 操作的值.
            if (mark.Equals("DBSrc_CondFrmEnum") == true)
            {
                return "SELECT IntKey as No, Lab as Name FROM Sys_Enum WHERE EnumKey='" + ht["Tag1"] + "' Order by IntKey";
            }
            //表单条件 CondFrmEnumString const 操作的值.
            if (mark.Equals("DBSrc_CondFrmEnumString") == true)
            {
                return "SELECT StrKey as No, Lab as Name FROM Sys_Enum WHERE EnumKey='" + ht["Tag1"] + "' Order by IntKey";
            }
            //请选择一个字典表 MapExt 请选择字典
            if (mark.Equals("DBSrc_MapExtSelectSFTable") == true)
            {
                return "SELECT No,Name FROM Sys_SFTable WHERE IsPara='" + ht["isPara"] + "'  AND No!='Blank' AND CodeStruct='" + ht["codeStruct"] + "' ";
            }
            //查询
            if (mark.Equals("DBSrc_SFSearch") == true)
            {
                return "SELECT No,Name FROM Sys_SFSearch";
            }
            //模板导入 GPN_FrmExpImp 导入表结构
            if (mark.Equals("DBSrc_SelectItemsByList") == true)
            {
                return "SELECT No, Name FROM Sys_SFDBSrc WHERE 1=1 AND DBSrcType!='WebApi'";
            }
            //级联下拉框 GPEActiveDDLSFTable 查询出来有参数的字典
            if (mark.Equals("DBSrc_ActiveDDLSFTable") == true)
            {
                return "SELECT No,Name FROM Sys_SFTable WHERE IsPara=1 ";
            }
            //级联下拉框 GPEAutoFullDDLSFTable 填充过滤 字典表
            if (mark.Equals("DBSrc_ActiveDDLDoc") == true)
            {
                return "SELECT No,Name FROM Sys_SFTable WHERE No!='Blank' AND IsPara=0 ";
            }
            //填充从表 FullDDLSFTable 查询
            if (mark.Equals("DBSrc_FullDDL") == true)
            {
                return "SELECT No, Name FROM Sys_SFTable WHERE IsPara=1 or IsPara=0  ";
            }
            //单实体平铺 PopList 字典表(有参1)
            if (mark.Equals("DBSrc_PopList") == true)
            {
                return "SELECT No,Name FROM Sys_SFTable WHERE No!='Blank' AND CodeStruct=0 ";
            }


            throw new Exception("err@没有判断的标记:" + mark);
        }

    }
}
